package com.agent.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import com.agent.comm.CommandProvider;
import com.agent.comm.ParamCommandProvider;

public class StartupUtil {
	private static final String COMMAND = ">>NoveCommand:";
	private static final String RESPONSE = "<<NoveResponse:\n";
	private ServerSocket ss;
	private int port;
	public StartupUtil() {
	}

	public StartupUtil(int port) {
		this.port = port;
		try {
			ss = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void startApplication(int port) {
		StartupUtil su = getInstance(port);
		su.start();
	}
	
	public static StartupUtil getInstance(int port) {
		StartupUtil su = null;
		su = new StartupUtil(port);
		return su;
	}
	
	public void start() {
		try {
			if (0 == port) {
				System.in.read();
			} else {
//				while (true) {
					new Thread(new Runnable() {
						@Override
						public void run() {
							BufferedReader input = null;
							PrintWriter out = null;
							try {
								while(true) {
								final Socket s = ss.accept();
								input = new BufferedReader(new InputStreamReader(s.getInputStream()));
								out = new PrintWriter(s.getOutputStream(), true);
								CommandProvider commandProvider = getCommandProvider();
								cc:while (true) {
									out.println(COMMAND);
									String str = input.readLine();
									if (null == str || "".equals(str.trim())) {
										continue;
									} else if ("disconnect".equalsIgnoreCase(str) || "bye".equalsIgnoreCase(str) || "quit".equalsIgnoreCase(str)) {
										break cc;
									} else {
										out.println("Command: {"+str+"}");
										String[] command = str.split(" ");
										if (3 != command.length) {
											out.println(RESPONSE + "command format error!!");
											out.println();
											continue;
										}

										String rsp = null;
										switch (command[0]) {
										case "start":
											rsp = commandProvider.start(command[1],command[2]);
											break;
										case "stop":
											rsp = commandProvider.stop(command[1]);
											break;
										default:
											rsp = "command only support start|stop";
										}

										out.println(RESPONSE + rsp);
										out.println();
										}
								}
								if (null != s) {
									try {
										s.close();
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
								}
							} catch (Exception e) {
								e.printStackTrace();
								if (null != out) {
									out.println(RESPONSE + e);
									out.println();
								}
							} finally {
								if (null != input) {
									try {
										input.close();
										input = null;
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
								if (null != out) {
									out.println(RESPONSE + "connect close!!");
									out.close();
									out = null;
								}}
							}

					}).start();
//				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("start utils success....");
	}
	
	private CommandProvider getCommandProvider() {
		// TODO Auto-generated method stub
		return new ParamCommandProvider();
	}
}

